package cn.yr.service.portal.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.yr.domain.entity.Tag;
import cn.yr.domain.pojo.ArticleTagPojo;
import cn.yr.domain.vo.ArticleTagVo;
import cn.yr.mapper.TagMapper;
import cn.yr.service.portal.TagService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * (Tag)表服务实现类
 *
 * @author makejava
 * @since 2020-03-11 19:54:41
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>  implements TagService {
    @Resource
    private TagMapper tagMapper;

    @Override
    public void setTag(ArticleTagVo articleTagVo){
        ArrayList<ArticleTagVo> articleTagVoArrayList = new ArrayList<>(1);
        articleTagVoArrayList.add(articleTagVo);
        setTag(articleTagVoArrayList);
    }

    @Override
    public void setTag(List<ArticleTagVo> list){
        //获取所有文字id
        List<Integer> ids = list.stream().map(article -> article.getId()).collect(Collectors.toList());
        QueryWrapper<ArticleTagPojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("article_id",ids);
        List<ArticleTagPojo> articleTagList = tagMapper.selectArticleTagList(queryWrapper);
        ConcurrentHashMap<Integer,List<ArticleTagPojo>> map = new ConcurrentHashMap<>();
        articleTagList.stream().collect(Collectors.groupingBy(ArticleTagPojo::getArticleId,Collectors.toList()))
                .forEach((articleId,fooListByArticleId)->{
                    map.put(articleId,fooListByArticleId);
                });
        list.forEach(articleTagVo -> {
            List<ArticleTagPojo> articleTagPojos = map.get(articleTagVo.getId());
            if(ObjectUtil.isNotEmpty(articleTagPojos)){
                articleTagVo.setArticleTagPojoList(articleTagPojos);
            }
        });
    }
}